<?php
$s=mysql_query('SELECT * FROM poll_otgovori');
if(mysql_num_rows($s)==0){
   echo '<p style="text-align: center;"><br /><br />';
   echo 'В момента няма качени анкети!</p>';
} else {
$select_art2=mysql_query('SELECT * FROM poll_vaprosi ORDER BY v_id DESC LIMIT 1');
$row_art2=mysql_fetch_assoc($select_art2);
$ip_u=$_SERVER["REMOTE_ADDR"];
$v_id=$row_art2['v_id'];
$select_ip=mysql_query('SELECT u_ip FROM ip WHERE u_ip="'.$ip_u.'" AND v_id='.$v_id.'');
if(mysql_num_rows($select_ip)==0){
?>
<form method="post" action="" style="position: relative; top: -15px;">
<?php
$select_art=mysql_query('SELECT * FROM poll_vaprosi ORDER BY v_id DESC LIMIT 1');
$row_art=mysql_fetch_assoc($select_art);
echo '<p style="font-weight: bold; text-align: center;">'.$row_art['vapros'].'</p>';
$select_otg=mysql_query('SELECT * FROM poll_otgovori WHERE v_id='.$row_art['v_id'].'');
while($row_otg=mysql_fetch_assoc($select_otg)){
   echo '<p><input type="radio" name="otgovor" value="'.$row_otg['otgovor'].'" style="margin-left: 15px; margin-top: 5px;"/>';
		   echo " ".$row_otg['otgovor']."</p>";
}
?>
<p>
<input type="submit" name="submit_poll" value="Гласувай" style="margin-top: 10px; width: 100px; height: 25px; border-radius: 5px; -moz-border-radius: 5px; background-color: #e4dcdc; color: #4d4a4a; margin-left: 5px; border: 1px solid #bbbbbb; color: gray;" />
<input type="hidden" name="hide_form" value="12"/>
<input type="hidden" name="hide_v" value="<?php echo $row_art['v_id']; ?>"/>
</p>
</form>
<?php 
} else {
$select_v=mysql_query('SELECT * FROM poll_vaprosi ORDER BY v_id DESC LIMIT 1');
$row_v=mysql_fetch_assoc($select_v);
echo '<p>';
echo '<br />';
echo '</p>';
echo '<p style="font-weight: bold; text-align: center; padding-bottom: 10px; position: relative; top: -30px;">'.$row_v['vapros'].'</p>';
$select_otgo=mysql_query('SELECT * FROM poll_otgovori WHERE v_id='.$row_v['v_id'].'');

$sql1='SELECT * FROM ip WHERE v_id='.$row_v['v_id'].''; 
$rez1=mysql_query($sql1); 
$broiip=mysql_num_rows($rez1); 

echo '<table border="0" style="margin-left: 20px; position: relative; top: -30px; ">';
while($row_otgo=mysql_fetch_assoc($select_otgo)){
	$koef = $row_otgo['rezultat']/$broiip; 
	$koef = $koef*2; 
	$koef = round($koef);
	
   echo "<tr>";
   echo "<td><p style='margin-left: 15px;'>".$row_otgo['otgovor']."</p></td>";
   echo "<td><img src=\"../img/poll/left.png\" alt=\"\" style=\"width: 1px; height: 9px; border: 0px;\" /><img src=\"../img/poll/center.png\" style=\"width:".$koef ."px; height:9px; max-width: 70px; border: 0px;\" alt=\"\" /><img src=\"../img/poll/right.png\" alt=\"\" style=\"width: 1px; height: 9px; border: 0px;\"/></td>"; 
   echo "<td><p style='margin-left: 15px;'>".$row_otgo['rezultat']."</p></td>";
   echo "</tr>";
}
echo '</table>';
}
if($_POST['hide_form']!=12) echo "";
else{
   if(empty($_POST['otgovor'])){
	 echo '<p style="margin-left: 10px;">Изберете опция!</p>';
   }
   else{
   $vupros=$_POST['hide_v'];
   $otgovor=$_POST['otgovor'];
   $ip=$_SERVER["REMOTE_ADDR"];
   $update_otgovori=mysql_query('UPDATE poll_otgovori SET rezultat=rezultat+1 WHERE otgovor="'.$otgovor.'"');
   $insert_ip=mysql_query('INSERT INTO ip(v_id, u_ip) VALUES("'.$vupros.'", "'.$ip.'")');
   echo '<p style="margin-left: 10px;">Успешен глас!</p>';
   echo '<meta http-equiv="refresh" content="0;url=index.php?name="'.$_GET['name'].'"">';
   }
}
}
?>